package com.company.woye.songbuState;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.company.woye.R;
import com.company.woye.onpackage.entity.BoxInfpEntity;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SongbuStateAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<RockBean.DataBean.RackDetailBean> listSonEntities;
    private Context context;

    public SongbuStateAdapter(Context context, List<RockBean.DataBean.RackDetailBean> listEntities) {
        this.inflater = LayoutInflater.from(context);
        this.listSonEntities = listEntities;
        this.context=context;
    }

    @Override
    public int getCount() {
        return listSonEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return listSonEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHodler = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.songbu_state_item, parent, false);
            viewHodler = new ViewHolder(convertView);
            convertView.setTag(viewHodler);
        } else {
            viewHodler = (ViewHolder) convertView.getTag();
        }
        RockBean.DataBean.RackDetailBean rackDetailBean = listSonEntities.get(position);

        viewHodler.tvDept.setText(rackDetailBean.getZb());
        viewHodler.labuNum.setText(rackDetailBean.getDh());
        viewHodler.tvOrder.setText(rackDetailBean.getHyh());
        viewHodler.tvGanghao.setText(rackDetailBean.getGh());
        viewHodler.tvColor.setText(rackDetailBean.getSc());
        //viewHodler.tvMatch.setText(rackDetailBean.get  );
        viewHodler.bupiCode.setText(rackDetailBean.getFabricNo());
        viewHodler.tvPihap.setText(rackDetailBean.getPbh());
        viewHodler.tvState.setText(rackDetailBean.getType());


        if (!TextUtils.isEmpty(rackDetailBean.getBackColor())){
            viewHodler.ll.setBackgroundColor(GetColorValue(rackDetailBean.getBackColor()));
        }
        if (!TextUtils.isEmpty(rackDetailBean.getFontColor())){
            viewHodler.tvDept.setTextColor(GetColorValue(rackDetailBean.getFontColor()));
            viewHodler.labuNum.setTextColor(GetColorValue(rackDetailBean.getFontColor()));
            viewHodler.tvOrder.setTextColor(GetColorValue(rackDetailBean.getFontColor()));
            viewHodler.tvGanghao.setTextColor(GetColorValue(rackDetailBean.getFontColor()));
            viewHodler.tvColor.setTextColor(GetColorValue(rackDetailBean.getFontColor()));
            viewHodler.tvMatch.setTextColor(GetColorValue(rackDetailBean.getFontColor()));
            viewHodler.bupiCode.setTextColor(GetColorValue(rackDetailBean.getFontColor()));
            viewHodler.tvPihap.setTextColor(GetColorValue(rackDetailBean.getFontColor()));
            viewHodler.tvState.setTextColor(GetColorValue(rackDetailBean.getFontColor()));
        }
        return convertView;
    }

    //从字符串转颜色
    public int GetColorValue(String paramColor){
        int PenColor= Color.WHITE;
        String StrPenColor=paramColor;
        if ((StrPenColor.indexOf("#")==0)&&(StrPenColor.length()==7)){
            int testColor=Integer.parseInt(StrPenColor.substring(1),16);
            int red = (testColor & 0xff0000) >> 16;
            int green = (testColor & 0x00ff00) >> 8;
            int blue = (testColor & 0x0000ff);
            PenColor=Color.rgb(red, green, blue);
        }else {
            if (isNumer(StrPenColor)) {
                int testColor = Integer.parseInt(StrPenColor);
                int red = (testColor & 0xff0000) >> 16;
                int green = (testColor & 0x00ff00) >> 8;
                int blue = (testColor & 0x0000ff);
                PenColor = Color.rgb(red, green, blue);
            } else {
                if (StrPenColor.equals("Black") == true)
                    PenColor = Color.BLACK;
                if (StrPenColor.equals("Blue") == true)
                    PenColor = Color.BLUE;
                if (StrPenColor.equals("Yellow") == true)
                    PenColor = Color.YELLOW;
                if (StrPenColor.equals("Red") == true)
                    PenColor = Color.RED;
                if (StrPenColor.equals("Green") == true)
                    PenColor = Color.GREEN;
                if (StrPenColor.equals("Gray") == true)
                    PenColor = Color.LTGRAY;
                if (StrPenColor.equals("White") == true)
                    PenColor = Color.WHITE;
            }
        }
        return PenColor;
    }

    //判断是否为数字，不然不转
    public static boolean isNumer(String str) {
        try {
            new BigDecimal(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    static
    class ViewHolder {
        @BindView(R.id.tv_dept)
        TextView tvDept;
        @BindView(R.id.labu_num)
        TextView labuNum;
        @BindView(R.id.tv_order)
        TextView tvOrder;
        @BindView(R.id.tv_ganghao)
        TextView tvGanghao;
        @BindView(R.id.tv_color)
        TextView tvColor;
        @BindView(R.id.tv_match)
        TextView tvMatch;
        @BindView(R.id.bupi_code)
        TextView bupiCode;
        @BindView(R.id.tv_pihap)
        TextView tvPihap;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.ll)
        LinearLayout ll;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
